package com.cljz.makepassenger.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseFragment;
import com.cljz.makepassenger.present.fragment.SettingMenuP;
import com.cljz.makepassenger.ui.activity.About1qActivity;
import com.cljz.makepassenger.ui.activity.IDSettingActivity;
import com.cljz.makepassenger.ui.activity.MainActivity;
import com.cljz.makepassenger.ui.activity.MessageSettingActivity;
import com.cljz.makepassenger.utils.FileUtils;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.widget.LetterDialogView;
import com.cljz.makepassenger.widget.SettingNotifyDialog;
import com.jakewharton.rxbinding.view.RxView;

import butterknife.BindView;
import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

import static com.cljz.makepassenger.base.BaseActivity.EXIT;

/**
 * Created by apple on 2017/4/24.
 */

public class SettingMenuFragment extends BaseFragment<SettingMenuP> {
    @BindView(R.id.ll_main)
    LinearLayout llMain;
    @BindView(R.id.ll_id_setting)
    LinearLayout llIdSetting;
    @BindView(R.id.ll_msg_setting)
    LinearLayout llMsgSetting;
    @BindView(R.id.ll_operation_guide)
    LinearLayout llOperationGuide;
    @BindView(R.id.ll_about_1q)
    LinearLayout llAbout1q;
    @BindView(R.id.ll_quit)
    LinearLayout llQuit;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    Unbinder unbinder;
    @BindView(R.id.tv_clearsize)
    TextView tvClearsize;
    @BindView(R.id.ll_clear_cache)
    LinearLayout llClearCache;
    private PopupWindow popupWindow;
    private LetterDialogView letterDialogView;
    StringBuilder dialogGuideText = new StringBuilder();
    private String letter;
    private static final String ATTR_PACKAGE_STATS="PackageStats";

    public static SettingMenuFragment newInstance() {
        SettingMenuFragment fragment = new SettingMenuFragment();
        return fragment;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        letter = "1.就是那个小圆圈，轻轻摸一下，马上创建您的邀请；\n"
                + "2.轻轻右划调整聚会时间，左滑即可快捷邀请；\n"
                + "3.还是那个小圆点，用力再按按。";
        String cacheSize = FileUtils.getFormatSize(FileUtils.getFileOrDirectorySize(App.getInstance().getCacheDir().getPath()));
        tvClearsize.setText("("+cacheSize+")");
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Intent intent = new Intent();
                        ((MainActivity) getActivity()).nvpMenu.setCurrentItem(0);
                    }
                });
        RxView.clicks(llIdSetting)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).to(IDSettingActivity.class).launch();
                    }
                });
        RxView.clicks(llMsgSetting)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).to(MessageSettingActivity.class).launch();
                    }
                });
        RxView.clicks(llOperationGuide)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showLetter("操作说明！", letter, "");
                    }
                });
        RxView.clicks(llClearCache)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                     showSettingWindow();
                    }
                });
        RxView.clicks(llAbout1q)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).to(About1qActivity.class).launch();
                    }
                });
        RxView.clicks(llQuit)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        final SettingNotifyDialog notifyDialog = new SettingNotifyDialog(App.getInstance(), (Activity) App.getInstance(), R.style.MyDialog, "SettingMenuFragment");
                        notifyDialog.show();
                        notifyDialog.mTxtDialogExit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                notifyDialog.dismiss();
                                getP().loginOut();
                                ((MainActivity) SettingMenuFragment.this.getActivity()).loginOvertime(EXIT);
                            }
                        });
                    }
                });
    }
    private void showSettingWindow() {
        View view = LayoutInflater.from(context).inflate(R.layout.pop_clearcache, null);
        View cancel = view.findViewById(R.id.tv_cancel);
        View ok = view.findViewById(R.id.tv_ok);

        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
     popupWindow.showAtLocation(llMain, Gravity.CENTER_VERTICAL, 0, 0);
       // popupWindow.showAsDropDown(llMain,0,0);
        RxView.clicks(cancel)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (popupWindow != null)
                            popupWindow.dismiss();
                    }
                });

        RxView.clicks(ok)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (popupWindow != null)
                            popupWindow.dismiss();
                        FileUtils.cleanInternalCache(context);
                        tvClearsize.setText("(0.0M)");
                    }
                });
    }
    @Override
    public int getLayoutId() {
        return R.layout.fragment_setting_menu;
    }

    @Override
    public SettingMenuP newP() {
        return new SettingMenuP();
    }


    //关于1Q的说明
    private void showLetter(String title, String text, String sign) {
        letterDialogView = new LetterDialogView(App.getInstance(), R.style.MyDialog, new LetterDialogView.YesDialogListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.iv_close:
                        letterDialogView.dismiss();
                        break;
                }
            }
        });
        letterDialogView.show();
        TextView tvLetterTitle = (TextView) letterDialogView.findViewById(R.id.tv_letter_title);
        TextView tvLetterText = (TextView) letterDialogView.findViewById(R.id.tv_letter_text);
        TextView tvLetterSign = (TextView) letterDialogView.findViewById(R.id.tv_letter_sign);
        tvLetterTitle.setText(title);
        tvLetterText.setText(text);
        tvLetterSign.setText(sign);
    }

    public void loginOutSucceed(String msg) {
        T.getInstance().showShort(msg);
    }


    @Override
    public void onResume() {
        super.onResume();
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

}
